<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use App\Models\ArticleCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Article(['category']), function (Grid $grid) {
            $grid->column('id')
                ->sortable();
            $grid->column('category.title', '分类');
            $grid->column('title');
            $grid->column('keywords');
            $grid->column('abstract');
            $grid->column('sort');
            $grid->column('cover_map')
                ->image('', 100, 150);
            $grid->column('is_footer', '是否页脚显示')
                ->switch();
            $grid->column('created_at');
            $grid->column('updated_at')
                ->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $articleCategoryModel = new ArticleCategory();
                // 更改为 panel 布局
                $filter->panel();
                // 展开过滤器
                $filter->expand();
                $filter->equal('id')
                    ->width(1);
                $filter->like('title', '标题')
                    ->width(2);
                $filter->equal('category_id', '类别')
                    ->select($articleCategoryModel->selectOptions())
                    ->width(2);
                $filter->between('created_at', '添加时间')
                    ->datetime()
                    ->width(4);
            });
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('title');
            $show->field('keywords');
            $show->field('abstract');
            $show->field('content');
            $show->field('sort');
            $show->field('cover_map');
            $show->field('is_footer');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Article(), function (Form $form) {
            $articleCategoryModel = new ArticleCategory();
            $form->display('id');
            $form->select('category_id', '分类')
                ->options($articleCategoryModel->selectOptions())
                ->saving(function ($v) {
                    return (int)$v;
                })
                ->required();
            $form->text('title')
                ->rules('required')
                ->required();
            $form->text('keywords')
                ->rules('required')
                ->required();
            $form->text('abstract')
                ->rules('required')
                ->required();
            $form->image('cover_map')
                ->autoUpload()
                ->removable(false)
                ->move('/article/' . date('Ymd'))
                ->uniqueName()
                ->saving(function ($v) {
                    return (string)$v;
                });
            $form->editor('content')
                ->rules('required')
                ->required();
            $form->number('sort');

            $form->switch('is_footer', '是否页脚显示')
                ->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
